package ru.picman.ui;

import java.util.ArrayList;
import java.util.List;

import ru.picman.KeyListener;
import ru.picman.R;
import ru.picman.util.ImageLibrary;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

public class PicmanView
             extends View
{
	
	public PicmanView(Context context, AttributeSet attrs) {
		this(context,attrs,0);
	}
	
	public PicmanView(Context context, AttributeSet attrs, int defStyle) {
		super(context,attrs,defStyle);
		
		listeners = new ArrayList<KeyListener>();
		paintables = new ArrayList<Paintable>();
		
		ImageLibrary.setResources(getResources());

		setFocusable(true);
	}
	
	public void addKeyListener(KeyListener listener) {
		if (listener == null)
			throw new NullPointerException();
		listeners.add(listener);
	}
	
	public void removeKeyListener(KeyListener listener) {
		listeners.remove(listener);
	}
	
	public void addPaintable(Paintable paintable) {
		if (paintable == null)
			throw new NullPointerException();
		paintables.add(paintable);
	}
	
	public void removePaintable(Paintable paintable) {
		paintables.remove(paintable);
	}
	
	
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for (Paintable paintable : paintables)
			paintable.paint(canvas,paint);
	}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		for (KeyListener listener : listeners)
			listener.onKeyDown(keyCode,event);
		
		return super.onKeyDown(keyCode,event);
	}
	
	//
	
	private final List<KeyListener> listeners;
	private final List<Paintable>   paintables;
	private final Paint             paint = new Paint();
	
	
	
	
}